﻿$(document).ready(function ()//
{
    $(".TurnPage").css("display", "none");
    $(".TurnPage").css("margin-left", $(window).width());
    $(".TurnPage").css("100%");

    $(".Back").click(back_click);
    $(".nav-pills .flip").click(TurnPage_click);
    $(window).on('resize', autoHeightBody);

    autoHeightBody();

});

ajaxConfig.onErro = function (r) { $.Loading(false); };
ajaxConfig.baseUrl = pGetURL();
ajaxConfig.showAlerta = false;
ajaxConfig.loadingGifUrl = Combine(ajaxConfig.baseUrl, "Content/Imagens/loader.gif");
TreeViewSettings.ImagemAbrir = Combine(ajaxConfig.baseUrl, "Content/Imagens/plus.gif");
TreeViewSettings.ImagemFechar = Combine(ajaxConfig.baseUrl, "Content/Imagens/minus.gif");
ajaxConfig.onErro = function (r) {

    $("body").append(r.ViewMensagem);

};

//Responsive Cuffon

function getCuffonFontSize(fontSize) {

    var percent = (fontSize * 100) / 1440;
    var windowSize = window.outerWidth;
    var newSize = (percent * windowSize) / 100;

    if (newSize > fontSize)
        return fontSize;
    else
        return newSize;
}

function autoHeightBody() {


    var atualWindowHeight = window.innerHeight;

    var atualPageHeight = $("html").height()
    
    var mainContentHeight = $("#MainContent").height();

    if(atualPageHeight < atualWindowHeight) {

    var newBodyHeight = (atualWindowHeight - atualPageHeight) + mainContentHeight + 21.5 ;

    $("#MainContent").css("height", newBodyHeight + "px");
    }
    
}

function pGetURL() {
    return "http://" + document.location.host + "/";
}

//eventos de paginação

$(document).ready(function () //
{
    $('body').on("click", ".pagination li", btnPaginar_Click);
    $('body').on("click", "ul li #login", login_click);
});


function login_click() {
    ShowLoginModal(window.location.href);
}

function ShowLoginModal(pReturnUrl) {

    $.BootModal({Titulo: "Login", Url: "account/ModalExternalLogin", Tamanho: 'lg', Dados: { returnUrl: pReturnUrl }, });
//    $.Modal(
//  {
//      Url: "/account/ModalExternalLogin",
//      Titulo: "Login",
//      Dados: { returnUrl: pReturnUrl },
//      OnLoad: function () { },
//      OnClose: function () { },
//      Width: '565px',
//      Height: '354px'
//  });
}

function CarregaMascaras() {
    $(".CampoTelefone").mask("(99) 9999-9999?9");
    $(".CampoValor").maskMoney({ thousands: '', decimal: ',' });
    var date = new Date();
    $(".CampoDataAtual").datepicker({
        showOn: "button",
        buttonImage: "../../../admin/Content/Imagens/calendario.png",
        buttonImageOnly: true
    }).val(date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear()).attr('readonly', 'readonly');

    $(".CampoDataAtual").mask("9?9/9?9/9999");

    $(".CampoData").datepicker({
        showOn: "button",
        buttonImage: "../../../admin/Content/Imagens/calendario.png",
        buttonImageOnly: true
    });
    $(".CampoData").mask("9?9/9?9/9999");
}


function back_click() {

    var marginLefty = $(".TurnPage[Active=Active]");
    $(marginLefty).removeAttr("Active");
    $(marginLefty).animate({
        marginLeft: parseInt($(marginLefty).css('marginLeft'), 10) == 0 ?
        $(marginLefty).outerWidth() : 0
    },
    function () { $(marginLefty).css("display", "none"); }
    );


}

function TurnPage_click(e) {

    //    if ($(window).height() < $("body").height())
    //        $(".TurnPage").css("height", $("body").height());
    //    else
    $(".TurnPage").css("100%");

    var marginLefty = undefined;
    switch ($(this).prop("id")) {
        case "contact": marginLefty = $("#Create"); break;
        case "about": marginLefty = $("#Invite"); break;
    }
    $(marginLefty).css("display", "block");
    $(marginLefty).attr("Active", "Active");

    $(marginLefty).animate({
        marginLeft: parseInt($(marginLefty).css('marginLeft'), 10) == 0 ?
        back_click() :
        0
    });
}

function btnPaginar_Click() {

    var Controller = $(this).attr('Controller');
    var Index = $(this).attr('Index');
    $.AjaxMVC(
            {
                Url: "/" + Controller + "/Pager",
                Json: false,
                Dados: "Index=" + Index
            });

}


(function ($) {
    jQuery.BootModal = function (config) {

        var _modal = {
            Url: undefined,
            Dados: undefined,
            Titulo: '',
            OnLoad: undefined,
            OnClose: undefined,
            Id_modal: Date.now(),
            Width: '400px',
            Height: '150px',
            SeletorFechar: undefined,
            Container: undefined,
            Tamanho: undefined
        };

        if (config)
            $.extend(_modal, config);

        if (_modal.Tamanho != undefined)
            _modal.Tamanho = "modal-" + _modal.Tamanho;
        else
            _modal.Tamanho = "";

        var Modal = {
            Show: function (_modal) {


                var IsJson = false;
                if (typeof _modal.Dados === "object") {
                    IsJson = true;
                }
                $.AjaxMVC(
                         {
                             Url: ajaxConfig.baseUrl + _modal.Url,
                             Json: IsJson,
                             Dados: _modal.Dados,
                             Type: 'GET',
                             Sucesso:
                            function (jsonResult) {
                                var title = _modal.Titulo;
                                title = jsonResult.Titulo != undefined ? jsonResult.Titulo : _modal.Titulo;
                                var id = _modal.Id_modal;
                                var HTML = jsonResult.View;

                                $('body').append('<div id="' + _modal.Id_modal + '" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block;"><div class="modal" style="display: block">  <div class="modal-dialog ' + _modal.Tamanho + '">    <div class="modal-content">      <div class="modal-header">        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>        <h4 class="modal-title">' + title + '</h4>      </div>      <div class="modal-body">        <p>' + HTML + '</p>      </div> </div><!-- /.modal-content -->  </div><!-- /.modal-dialog --></div></div>');
                                $('#' + _modal.Id_modal).modal({ show: true });

                                if (_modal.OnLoad != undefined)
                                    _modal.OnLoad();

                                $('#' + _modal.Id_modal).on('hidden.bs.modal', function (e) {
                                    if (_modal.OnClose != undefined)
                                        _modal.OnClose();

                                    $('#' + _modal.Id_modal).remove();
                                })

                            }
                         });

            }

        };

        Modal.Show(_modal);
        return Modal;
    };
})(jQuery);